<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="字典管理">
<style>
    .form-horizontal .controls {
        line-height: 40px;
        height: 40px;
    }

    .button-primary {
        margin-left: 5px;
    }
</style>
<form id="searchForm" class="form-panel" action="${basepath}/manage/dictionary/loadData">
    <ul class="panel-content">
        <li>
            <div class="control-group span5">
                <div class="search-controls  controls">
                    <input type="text" name="dictName" id="title" value="" placeholder="搜索类型、名称">
                </div>
            </div>
            <div class="form-actions span2">
                <button type="submit" class="button  button-primary">
                    搜索
                </button>
            </div>
            <div class="form-actions span2">
                <#--<a href="${basepath}/manage/dictionary/toAdd" class="button button-success">添加</a>-->
                    <a class="button button-success" href="javascript:add();" >添加</a>
            </div>
            <div class="form-actions span2">
                <a class="button button-danger" href="javascript:delFunction();">批量删除</a>
            </div>
        </li>
    </ul>
</form>
<div id="grid"></div>
<#---------------------------------------添加表单start------------------------------------------------->
<div id="addContent" style="display:none;">
    <form id="addForm" class="form-horizontal" action="${basepath}/manage/dictionary/insertJson" method="post">
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">字典类型：</label>

                <div class="controls">
                    <input type="text" class="input-normal control-text" name="dictType" data-rules="{required : true}">
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label">类型名称：</label>

                <div class="controls">
                    <input type="text" class="input-normal control-text" name="dictName" data-rules="{required : true}">
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label">名称值：</label>

                <div class="controls">
                    <input type="text" class="input-normal control-text" name="dictValue" data-rules="{required : true}">
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label">顺序：</label>

                <div class="controls">
                    <input type="text" class="input-normal control-text" name="dictOrder" data-rules="{required : true}">
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label">显示状态：</label>

                <div class="controls">
                    <select name="status" data-rules="{required : true}">
                        <option value="">-请选择-</option>
                        <option value="1">显示</option>
                        <option value="0">隐藏</option>
                    </select>
                </div>
            </div>
        </div>
    </form>
</div>
<#----------------------------------------添加表单end---------------------------------------------------->
<script>
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '序号', dataIndex: 'id', elCls: 'center', width: '10%'},
                {title: '字典类型', dataIndex: 'dictType', elCls: 'center', width: '15%'},
                {title: '名称值', dataIndex: 'dictValue', elCls: 'center', width: '15%'},
                {title: '类型名称', dataIndex: 'dictName', elCls: 'center', width: '15%'},
                {title: '顺序', dataIndex: 'dictOrder', elCls: 'center', width: '15%'},
                {
                    title: '状态', dataIndex: 'status', elCls: 'center', width: '15%', renderer: function (obj) {
                    if (obj == "1") {
                        return '已显示';
                    } else {
                        return '已隐藏';
                    }
                }
                },
                {
                    title: '操作', dataIndex: 'id', elCls: 'center', width: '15%', renderer: function (data,obj) {
                    var option3;
                    if (obj.status == "1") {
                        option3='<a href="javascript:changeStatus(0);">隐藏</a>';
                    } else {
                        option3='<a href="javascript:changeStatus(1);">显示</a>';
                    }

                    var option1 = '<a  href="${basepath}/manage/dictionary/toEdit?id=' + data + '">更新</a>';
                    var option2 = '<a  href="javascript:delFunction();">删除</a>';
                    return option3+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +option1 + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + option2;
                }
                }
            ];
    var store = new Store({
                url: 'loadData',
                autoLoad: true,                //自动加载数据
                params: {                       //配置初始请求的参数
                    length: '10',
                    status: '1'
                },
                pageSize: 10,	                // 配置分页数目
                root: 'list',
                totalProperty: 'total'          //加载数据时，返回记录的总数的字段，用于分页

            }),
            grid = new Grid.Grid({
                render: '#grid',
                columns: columns,
                loadMask: true,                             //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection],     // 插件形式引入多选表格

                // 表格底部工具栏，用于下一页，上一页，跳转到指定页
                bbar: {
                    pagingBar: true
                }
            }).render();
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        ///序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0;                      //返回第一页
        store.load(obj);
        return false;
    });

    //批量改变字典数据的状态
    function changeStatus(type) {
        var selections = grid.getSelection();
        var ids = new Array();
        for (var i = 0; i < selections.length; i++) {
            ids[i] = selections[i].id.toString()
        }
        $.ajax({
            type: "POST",
            url: "${basepath}/manage/dictionary/changeStatus",
            dataType: "json",
            data: {
                idArr: ids,
                type: type
            },
            success: function (data) {
                var obj = form.serializeToObject(); //将表单格式化成键值对形式
                obj.start = 0; //返回第一页
                store.load(obj); //重新加载表单
            }
        });
    }
    /**
     * 删除选中记录
     */
    function delFunction() {
        var selections = grid.getSelection();
        var ids = new Array();
        //遍历选择的每一个条数
        for (var i = 0; i < selections.length; i++) {
            ids[i] = selections[i].id.toString()
        }
        $.ajax({
            type: "POST",
            url: "${basepath}/manage/dictionary/deletesJson",               //删除操作
            dataType: "json",
            data: {
                ids: ids                                                  //这个值要后台对应
            },
            success: function (data) {
                var obj = form.serializeToObject();
                obj.start = 0;                                              //返回第一页
                store.load(obj);
            }
        });
    }
    /*-------------------------------添加表单start------------------------------------*/
    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType: 'ajax',
        callback: function () {
           /* var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj); //重新从第一页开始加载数据*/
            addDialog.close();  //表单提交成功，弹出框关闭
        }
    }).render();
    //创建一个弹出框,buttons属性@default '确定'、'取消'2个按钮
    var addDialog = new BUI.Overlay.Dialog({
        title: '添加字典',
        width: 400,
        height: 300,
        contentId: 'addContent',  //弹出框显示内容的DOM容器ID，对应包围form表单div的id
        success: function () {
           /* addForm.ajaxSubmit();*/
            //判断表单是否通过验证
            if (addForm.isValid()) {
                addForm.ajaxSubmit();
                window.location.href="${basepath}/manage/dictionary/toList";

            } else {
                addForm.valid();
            }
        }
    });
    //添加按钮事件
    function add() {
        /*addForm.clearFields(); *///清除弹出框中所有表单域的值
        addDialog.show();  //弹出框显示
    }
    /*----------------------------添加表单end---------------------------------------*/
</script>
</@page.pageBase>